package com.wy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wy.controller.dto.AttributeExamDTO;
import com.wy.entity.CheckAttribute;
import com.wy.entity.PetAuxiliaryCheck;
import com.wy.service.ICheckAttributeService;
import com.wy.service.IDiagnosisService;
import com.wy.service.IPetAuxiliaryCheckService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wy.common.R;

import com.wy.service.IAttributeExamService;
import com.wy.entity.AttributeExam;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 王昭贵
 * @since 2023-04-27
 */
@RestController
@RequestMapping("/attribute-exam")
public class AttributeExamController {

    @Resource
    private IAttributeExamService attributeExamService;
    @Resource
    private IPetAuxiliaryCheckService petAuxiliaryCheckService;

    @Resource
    private ICheckAttributeService checkAttributeService;
    @Resource
    private IDiagnosisService diagnosisService;
    /****
     * 提交检查数据
     * @param attributeExamDTO
     * @return
     */
    @Transactional
    @PostMapping("publishexam")
    public R publishExam(@RequestBody AttributeExamDTO attributeExamDTO) {

        AttributeExam attributeExam = new AttributeExam();
        if (attributeExamDTO.getAuxiliaryCheckId() == 1) {
            attributeExam = new AttributeExam();
            attributeExam.setPetAuxiliaryCheckId(attributeExamDTO.getPetAuxiliaryCheckId());
            attributeExam.setAttributeName("白细胞计数");
            attributeExam.setExam(attributeExamDTO.getBxb());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName(" 红细胞计数");
            attributeExam.setExam(attributeExamDTO.getHxb());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName("血红蛋白浓度");
            attributeExam.setExam(attributeExamDTO.getXhdb());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName("血小板计数");
            attributeExam.setExam(attributeExamDTO.getXxb());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName("血糖浓度");
            attributeExam.setExam(attributeExamDTO.getXtnd());
            attributeExamService.save(attributeExam);
        } else if (attributeExamDTO.getAuxiliaryCheckId() == 2) {
            attributeExam = new AttributeExam();
            attributeExam.setPetAuxiliaryCheckId(attributeExamDTO.getPetAuxiliaryCheckId());
            attributeExam.setAttributeName("超声描述");
            attributeExam.setExam(attributeExamDTO.getCsmm());
            attributeExamService.save(attributeExam);
        } else if (attributeExamDTO.getAuxiliaryCheckId() == 3) {
            attributeExam = new AttributeExam();
            attributeExam.setPetAuxiliaryCheckId(attributeExamDTO.getPetAuxiliaryCheckId());
            attributeExam.setAttributeName("尿比重");
            attributeExam.setExam(attributeExamDTO.getNbz());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName("尿PH值");
            attributeExam.setExam(attributeExamDTO.getNph());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName("尿蛋白");
            attributeExam.setExam(attributeExamDTO.getNdb());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName("尿酮体");
            attributeExam.setExam(attributeExamDTO.getNtt());
            attributeExamService.save(attributeExam);
            attributeExam.setAttributeName("尿糖");
            attributeExam.setExam(attributeExamDTO.getNt());
            attributeExamService.save(attributeExam);
        }
        PetAuxiliaryCheck petAuxiliaryCheck = petAuxiliaryCheckService.getById(attributeExamDTO.getPetAuxiliaryCheckId());
        petAuxiliaryCheck.setCheckUserId(attributeExamDTO.getCheckUserId());
        petAuxiliaryCheck.setStatus(3);
        petAuxiliaryCheckService.updateById(petAuxiliaryCheck);
        return R.success("结果发布成功");

    }
    /****
     * 后台查看辅助检查记录--单项
     * @param id
     * @return
     */
    @GetMapping("/bypetauxiliarycheck/{id}")
    public R findOneByPetAuxiliaryCheckId(@PathVariable Integer id) {
        LambdaQueryWrapper<AttributeExam> attributeExamLambdaQueryWrapper = new LambdaQueryWrapper<>();
        attributeExamLambdaQueryWrapper.eq(AttributeExam::getPetAuxiliaryCheckId, id);
        List<AttributeExam> list = attributeExamService.list(attributeExamLambdaQueryWrapper);
        list.forEach(item -> {
            CheckAttribute one = checkAttributeService.getOne(new LambdaQueryWrapper<CheckAttribute>().eq(CheckAttribute::getName, item.getAttributeName()));
            item.setStandardValue(one.getStandardValue());
        });
        return R.success(list);
    }
    /****
     * 通过问诊记录id、辅助检查类型id获得检查记录
     * @param diagnosisId
     * @param auxiliaryCheckId
     * @return
     */
    @GetMapping("/byDiagnosis")
    public R findByDiagnosisIdAndAuxiliaryId(@RequestParam Integer diagnosisId, Integer auxiliaryCheckId) {
        LambdaQueryWrapper<PetAuxiliaryCheck> petAuxiliaryCheckLambdaQueryWrapper = new LambdaQueryWrapper<>();
        petAuxiliaryCheckLambdaQueryWrapper.eq(PetAuxiliaryCheck::getDiagnosisId, diagnosisId);
        petAuxiliaryCheckLambdaQueryWrapper.eq(PetAuxiliaryCheck::getAuxiliaryCheckId, auxiliaryCheckId);
        PetAuxiliaryCheck one = petAuxiliaryCheckService.getOne(petAuxiliaryCheckLambdaQueryWrapper);
        List<AttributeExam> list = attributeExamService.list(new LambdaQueryWrapper<AttributeExam>().eq(AttributeExam::getPetAuxiliaryCheckId, one == null ? "" : one.getId()));
        list.forEach(item -> {
            CheckAttribute checkAttribute = checkAttributeService.getOne(new LambdaQueryWrapper<CheckAttribute>().eq(CheckAttribute::getName, item.getAttributeName()));
            item.setStandardValue(checkAttribute.getStandardValue());
        });
        return R.success(list);
    }

}

